package net.thxjj.mobileServer.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import net.thxjj.mobileServer.controller.base.BaseController;
import net.thxjj.mobileServer.entity.DataBean;
import net.thxjj.mobileServer.entity.ResponseModel;
import net.thxjj.mobileServer.entity.ResultEnum;
import net.thxjj.mobileServer.service.IUserService;
import net.thxjj.mobileServer.util.ParameterMap;
import net.thxjj.mobileServer.util.Tools;
import net.thxjj.mobileServer.util.sampleUtil.NonBlockingPublisher;
import net.thxjj.mobileServer.websocket.AppSocketMap;
import net.thxjj.mobileServer.websocket.WebsocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @ClassName:MobileUaaController
 * @Description:TODO
 * @Autor:TracyHenry
 * @Date:2022/12/18 0018
 * @Version:1.0
 **/
@Slf4j
@RestController
@RequestMapping("/")
public class LoginController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * login
     * @return
     * 终端登录接口
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Object login(@RequestBody JSONObject userLogin){
        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("username",userLogin.getString("username"));
        parameterMap.put("password",userLogin.getString("password"));
        parameterMap.put("pid",userLogin.getString("pid"));
        parameterMap.put("app_tid",userLogin.getString("app_tid"));
        parameterMap.put("login_type",userLogin.getString("login_type"));
        parameterMap.put("rip",getParameterMap().getString("rip"));

        return userService.Login(parameterMap);
    }


    /**
     * refreshToken
     * @return
     * 移动端刷新access_token
     */
    @RequestMapping(value = "/refreshToken",method = RequestMethod.POST)
    @ResponseBody
    public Object refreshToken(@RequestBody JSONObject jsonObject){

        String refresh_token = jsonObject.getString("refresh_token");

        String user_id = jsonObject.getString("user_id");

        if(Tools.isEmpty(refresh_token)||Tools.isEmpty(user_id)){
            return ResponseModel.getModel(ResultEnum.PARAMS_EMPTY_ERROR,null);
        }

        if(!Tools.isNumber(user_id)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_ERROR,null);
        }

        return userService.refreshToken(refresh_token,Long.parseLong(user_id));
    }




    /**
     * refreshToken
     * @return
     * 移动端刷新access_token
     */
    @RequestMapping(value = "/Test",method = RequestMethod.GET)
    @ResponseBody
    public Object Test(){
        String aa = "{\"hw_id\":\"000c2933eaa6\",\"msg\":{\"CMD_CODE\":1,\"str1\":\"3769000603\",\"str2\":\"010D01\",\"str3\":\"\"},\"msg_id\":13,\"namespace\":\"config\",\"source\":\"app\",\"target\":\"gateway\"}";


        DataBean dataBean = JSONObject.parseObject(aa,DataBean.class);



        Thread blockingPublishThread = new Thread(new NonBlockingPublisher(WebsocketServer.awsIotClient,dataBean));
        blockingPublishThread.start();

        return ResponseModel.getModel(ResultEnum.SUCCESS,null);

    }


}
